import pygame
from pygame.locals import *
import time

pygame.init()
"""Variables"""
selection=0
SW,SH = 656,416
FPS=30
selectedScreen=0
clock = pygame.time.Clock()
def date_to_string():
    return time.strftime("%d %b %Y",time.localtime())
def time_to_string():
    return time.strftime("%H:%M",time.localtime())
def display_text(screen, message,size,x,y):    
    if len(message) != 0:
        fontobject=pygame.font.SysFont('Arial', size)
        screen.blit(fontobject.render(message, 1, (255, 255, 255)),(x,y))
if __name__ == '__main__':
    date = date_to_string()
    """Screen Settings""" 
    screen = pygame.display.set_mode((SW, SH))
    pygame.display.set_caption('Car PC')
    pygame.mouse.set_visible(False)

    
    """Image Loads"""
    backgroundImage =  pygame.image.load("../pictures/background.jpg").convert()
    backgroundImage =  pygame.transform.scale(backgroundImage,(SW,SH))
    weatherImage = pygame.image.load("../pictures/weather.png").convert_alpha()
    weatherImageTumb = pygame.transform.scale(weatherImage,(75,75))
    weatherImage = pygame.transform.scale(weatherImage,(200,200))
    rssImage = pygame.image.load("../pictures/rss.png").convert_alpha()
    rssImageTumb = pygame.transform.scale(rssImage,(75,75))
    rssImage = pygame.transform.scale(rssImage,(200,200))
    emailImage = pygame.image.load("../pictures/email.png").convert_alpha()
    emailImageTumb = pygame.transform.scale(emailImage,(75,75))
    emailImage = pygame.transform.scale(emailImage,(200,200))
    
    
    _quit = False
    while not _quit:
        for e in pygame.event.get():
            if e.type is QUIT: _quit = True
            if e.type is KEYDOWN and e.key == K_ESCAPE: _quit = True
            if e.type is KEYDOWN and e.key == K_RIGHT:
                if selectedScreen==0:
                    selection = (selection+1) %3
            if e.type is KEYDOWN and e.key == K_LEFT:
                if selectedScreen==0:
                    selection = (selection+2)%3
            if e.type is KEYDOWN and e.key == K_DOWN:
                if selectedScreen==0:
                    selectedScreen=selection+1
                    print ("Menu: "+str(selection))
            if e.type is KEYDOWN and e.key == K_a:
                selectedScreen=0
        
        if selectedScreen==0:
            screen.blit(backgroundImage,(0,0))
            screen.blit(weatherImageTumb,(50,15))
            screen.blit(rssImageTumb,(160,15))
            screen.blit(emailImageTumb,(270,15))
            
            if selection==0:
                screen.blit(weatherImage,(SW/2-100,SH/2-100))
                pygame.draw.rect(screen, Color("white"), pygame.Rect(45, 15, 90, 90), 2)
                display_text(screen,"Weather",50,SW/2-100, 300)
            elif selection==1:
                screen.blit(rssImage,(SW/2-100,SH/2-100))
                pygame.draw.rect(screen, Color("white"), pygame.Rect(155, 15, 90, 90), 2)
                display_text(screen,"RSS Feeds",50,SW/2-100, 300)
            elif selection==2:
                screen.blit(emailImage,(SW/2-100,SH/2-100))
                pygame.draw.rect(screen, Color("white"), pygame.Rect(265, 15, 90, 90), 2)
                display_text(screen,"E-Mail Box",50,SW/2-100, 300)
            display_text(screen,date,25,0,SH-50)
            display_text(screen,time_to_string(),20,85,SH-25)
        
        
        
        elif selectedScreen==1:
            screen.blit(backgroundImage,(0,0))
            display_text(screen,"Weather",20,330,280)
            display_text(screen,date,25,0,SH-50)
            display_text(screen,time_to_string(),20,85,SH-25)
        elif selectedScreen==2:
            screen.blit(backgroundImage,(0,0))
            display_text(screen,"RSS",20,330,280)
            display_text(screen,date,25,0,SH-50)
            display_text(screen,time_to_string(),20,85,SH-25)          
        elif selectedScreen==3:
            screen.blit(backgroundImage,(0,0))
            display_text(screen,"EMAiL",20,330,280)
            display_text(screen,date,25,0,SH-50)
            display_text(screen,time_to_string(),20,85,SH-25)         
        pygame.display.flip()
        clock.tick(FPS)

